package d集合.day2.作业;

import java.util.Arrays;

public class MyArr<T> {
    Object[]arr;
    public MyArr(){
        arr=new Object[0];
    }
    int size;

    public void add(T t){
        if(size==arr.length){
            arr= Arrays.copyOf(arr,arr.length+arr.length>>1);
        }
        arr[size++]=t;
    }
    public T get(int index){
        if(index>size-1||index<0){
            throw new ArrayIndexOutOfBoundsException(index);
        }
        return (T)arr[index];
    }

    public int size(){
        return size;
    }

    @Override
    public String toString() {
        StringBuilder sb=new StringBuilder("[");
        for(Object o:arr){
            if(o==null){
                break;
            }
            sb.append(o.toString()+",");
        }
        sb.replace(sb.length()-1,sb.length(),"]");
        return sb.toString();
    }
}
